#!/bin/ash
#---------------------------------------------------------------- 
# Shell Name：Monitor
# Description：Plug-in monitor script
# Author：Starry
# E-mail: starry@misstar.com
# Time：2016-11-06 02:30 CST
# Copyright © 2016 Misstar Tools. All rights reserved.
#----------------------------------------------------------------*/
START=99
STOP=99
. /etc/misstar/scripts/MTbase
rm /etc/dnsmasq.conf.disabled.*

#misstar-koolproxy
App_enable=$(uci get misstar.koolproxy.enable) #misstar-koolproxy
if [ "$App_enable" = '1' ];then #misstar-koolproxy
	result1=$(ps | grep koolproxy | grep -v grep | grep -v script | wc -l) #misstar-koolproxy
	result2=$(iptables -L -t nat  | grep KOOLPROXY | wc -l) #misstar-koolproxy
	if [ "$result1" == "0" ] || [ "$result2" == "0" ]; then #misstar-koolproxy
		MTlog 3 "KoolProxy Process state Error,Try to restart it..." #misstar-koolproxy
		/etc/misstar/applications/koolproxy/script/koolproxy restart #misstar-koolproxy
	fi #misstar-koolproxy
fi #misstar-koolproxy

App_enable=$(uci get misstar.ss.enable)  #misstar-ss
if [ "$App_enable" = '1' ];then  #misstar-ss
	result1=$(ps | grep -E "ss-redir|ssr-redir" | grep -v 'grep' | grep -v script | wc -l)  #misstar-ss
	result2=$(iptables -L -t nat  | grep SHADOWSOCKS | wc -l)
	if [ "$result1" == "0" ] || [ "$result2" == "0" ]; then #misstar-ss
		MTlog 3 "SS Process state Error,Try to restart it..."  #misstar-ss
		/etc/misstar/applications/ss/script/ss restart  #misstar-ss
	fi  #misstar-ss
	i=1  #misstar-ss
	while :  #misstar-ss
	do  #misstar-ss
		ss_status=$(/etc/misstar/applications/ss/script/ss status)   #misstar-ss
		if [ "$ss_status" = '2' ];then  #misstar-ss
			break  #misstar-ss
		fi  #misstar-ss
		sleep 10  #misstar-ss
		if [ "$i" = '3' ];then #misstar-ss
			/etc/misstar/applications/ss/script/ss restart  #misstar-ss
			break #misstar-ss
		fi  #misstar-ss
		i=`expr $i + 1`  #misstar-ss
	done  #misstar-ss
fi #misstar-ss
#misstar-ftp
App_enable=$(uci get misstar.ftp.enable) #misstar-ftp
if [ "$App_enable" = '1' ];then #misstar-ftp
	result=$(ps | grep vsftpd | grep -v grep | grep -v script | wc -l) #misstar-ftp
	if [ "$result" == "0" ]; then #misstar-ftp
		MTlog 3 "VSFTPD Process state Error,Try to restart it..."  #misstar-ftp
		/etc/misstar/applications/ftp/script/vsftpd restart #misstar-ftp
	fi #misstar-ftp
fi #misstar-ftp